///////////////////////////////////////////////////////
// 小车的集装箱有以下的负荷模式:
//      Container1  Container2
// 0:       X           X
// 1:       20          X
// 2:       X           20
// 3:       20          20
// 4:       40 
// 5:       45
////////////////////////////////////////////////////////


import Icon from 'ol/style/Icon';
import Point from 'ol/geom/Point';
import Style from 'ol/style/Style';
import Feature from 'ol/Feature';

const SCALE_AHT = 1.0;
const SCALE_CONTAINER = 0.13;
const BASE_ZOOM_LEVEL = 10.0;

const getScaleFactor = (currentZommLevel, baseZoomLevel) => Math.pow(2, currentZommLevel - baseZoomLevel);

export class AHT {

    constructor(view, layer) {

        // Property
        this.coordinate = [0.0, 0.0];
        this.rotation = 0.0;
        this.scale = 1.0;
        this.mode = 0;

        // Context
        this.view = view;
        this.layer = layer;
        this.source = layer.getSource();

        this.geometry = undefined;
        this.icons = { aht: undefined, container1: undefined, container2: undefined, container3: undefined, container4: undefined };
        this.styles = { aht: undefined, container1: undefined, container2: undefined, container3: undefined, container4: undefined };
        this.features = { aht: undefined, container1: undefined, container2: undefined, container3: undefined, container4: undefined };

        // 初始化
        this.#createAht();

        // 更新初始化参数
        this.updateMode();
        this.updateCoordinate();
        this.updateRotation();
        this.updateScale();

    }

    /** 创建AHT的ICON图形, 对象指针 */
    #createAht() {
        const geometry = new Point(this.coordinate);
        this.geometry = geometry;

        {
            const feature = new Feature({ geometry: geometry });
            const icon = new Icon({ src: "/aht.png", anchor: [0.5, 0.5], rotation: 0.0 });
            const style = new Style({ image: icon, zIndex: 1 });
            feature.setStyle(style);
            feature.updateScale = () => {
                const currentZommLevel = this.view.getZoom();
                const scaleRes = this.scale * SCALE_AHT * getScaleFactor(currentZommLevel, BASE_ZOOM_LEVEL);
                icon.setScale(scaleRes);
            }
            this.source.addFeature(feature);
            this.features.aht = feature;
            this.icons.aht = icon;
            this.styles.aht = style;
        }


        {
            const feature = new Feature({ geometry: geometry });
            const icon = new Icon({ src: "/container-border.png", anchor: [0.5, 1.1], rotation: 0.0 });
            const style = new Style({ image: icon, zIndex: 2 });
            feature.setStyle(style);
            feature.updateScale = () => {
                const currentZommLevel = this.view.getZoom();
                const scaleRes = this.scale * SCALE_CONTAINER * getScaleFactor(currentZommLevel, BASE_ZOOM_LEVEL);
                icon.setScale(scaleRes);
            }
            this.source.addFeature(feature);
            this.features.container1 = feature;
            this.icons.container1 = icon;
            this.styles.container1 = style;
        }

        {
            const feature = new Feature({ geometry: geometry });
            const icon = new Icon({ src: "/container-border.png", anchor: [0.5, -0.15], rotation: 0.0 });
            const style = new Style({ image: icon, zIndex: 2 });
            feature.setStyle(style);
            feature.updateScale = () => {
                const currentZommLevel = this.view.getZoom();
                const scaleRes = this.scale * SCALE_CONTAINER * getScaleFactor(currentZommLevel, BASE_ZOOM_LEVEL);
                icon.setScale(scaleRes);
            }
            this.source.addFeature(feature);
            this.features.container2 = feature;
            this.icons.container2 = icon;
            this.styles.container2 = style;
        }


        {
            const feature = new Feature({ geometry: geometry });
            const icon = new Icon({ src: "/containerL-border.png", anchor: [0.5, 0.5], rotation: 0.0 });
            const style = new Style({ image: icon, zIndex: 2 });
            feature.setStyle(style);
            feature.updateScale = () => {
                const currentZommLevel = this.view.getZoom();
                const scaleRes = this.scale * SCALE_CONTAINER * getScaleFactor(currentZommLevel, BASE_ZOOM_LEVEL);
                icon.setScale(scaleRes);
            }
            this.source.addFeature(feature);
            this.features.container3 = feature;
            this.icons.container3 = icon;
            this.styles.container3 = style;
        }


        {
            const feature = new Feature({ geometry: geometry });
            const icon = new Icon({ src: "/containerLL-border.png", anchor: [0.5, 0.5], rotation: 0.0 });
            const style = new Style({ image: icon, zIndex: 2 });
            feature.setStyle(style);
            feature.updateScale = () => {
                const currentZommLevel = this.view.getZoom();
                const scaleRes = this.scale * SCALE_CONTAINER * getScaleFactor(currentZommLevel, BASE_ZOOM_LEVEL);
                icon.setScale(scaleRes);
            }
            this.source.addFeature(feature);
            this.features.container4 = feature;
            this.icons.container4 = icon;
            this.styles.container4 = style;
        }
    }

    /** 更新当前运载负荷 */
    updateMode() {
        switch (this.mode) {
            case 0:
                this.features.container1.setStyle(null);
                this.features.container2.setStyle(null);
                this.features.container3.setStyle(null);
                this.features.container4.setStyle(null);
                break;
            case 1:
                this.features.container1.setStyle(this.styles.container1);
                this.features.container2.setStyle(null);
                this.features.container3.setStyle(null);
                this.features.container4.setStyle(null);
                break;
            case 2:
                this.features.container1.setStyle(null);
                this.features.container2.setStyle(this.styles.container2);
                this.features.container3.setStyle(null);
                this.features.container4.setStyle(null);
                break;
            case 3:
                this.features.container1.setStyle(this.styles.container1);
                this.features.container2.setStyle(this.styles.container2);
                this.features.container3.setStyle(null);
                this.features.container4.setStyle(null);
                break;
            case 4:
                this.features.container1.setStyle(null);
                this.features.container2.setStyle(null);
                this.features.container3.setStyle(this.styles.container3);
                this.features.container4.setStyle(null);
                break;
            case 5:
                this.features.container1.setStyle(null);
                this.features.container2.setStyle(null);
                this.features.container3.setStyle(null);
                this.features.container4.setStyle(this.styles.container4);
                break;
        }
    }

    /** 更新缩放 */
    updateScale() {
        this.features.aht.updateScale();
        this.features.container1.updateScale();
        this.features.container2.updateScale();
        this.features.container3.updateScale();
        this.features.container4.updateScale();
    }

    /** 更新坐标 */
    updateCoordinate() {
        this.geometry.setCoordinates(this.coordinate);
    }

    /** 更新旋转 */
    updateRotation() {
        this.icons.aht.setRotation(this.rotation);
        this.icons.container1.setRotation(this.rotation);
        this.icons.container2.setRotation(this.rotation);
        this.icons.container3.setRotation(this.rotation);
        this.icons.container4.setRotation(this.rotation);
    }

    setMode(modeI = 0) {
        this.mode = modeI;
        this.updateMode();
    }

    setSacle(scaleI = 1.0) {
        this.scale = scaleI;
        this.updateScale();
    }

    setCoordinate(coordinateI = [0.0, 0.0]) {
        this.coordinate[0] = coordinateI[0];
        this.coordinate[1] = coordinateI[1];
        this.updateCoordinate();
    }

    setRotation(rotationI = 0.0) {
        this.rotation = rotationI;
        this.updateRotation();
    }

}